<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职工姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入职工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table border stripe v-loading="loading" :data="workImgList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="编号" width="60" align="center" prop="id"/>
      <el-table-column label="部门名称" width="150" show-overflow-tooltip align="center" prop="deptName"/>
      <el-table-column label="职工姓名" width="150" show-overflow-tooltip align="center" prop="nickName"/>
      <el-table-column label="头像" width="100" show-overflow-tooltip align="center" prop="nickName">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="内容" show-overflow-tooltip align="center" prop="content"/>
      <el-table-column label="照片" width="100" align="center" prop="images">
        <template slot-scope="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="160" show-overflow-tooltip align="center" prop="createTime"/>
      <el-table-column label="积分奖励" width="100" align="center" prop="gradeTime">
        <template slot-scope="scope">
          <div v-if="scope.row.isGrade == 1">
            <i class="el-icon-success" style="color: green;font-size: 16px"></i>
          </div>
          <div v-else>
            <i class="el-icon-error" style="color: #909399;font-size: 16px"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="更新账号" width="160" show-overflow-tooltip align="center" prop="updateBy"/>
      <el-table-column label="更新时间" width="160" show-overflow-tooltip align="center" prop="updateTime"/>
      <el-table-column label="状态" width="100" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column width="150" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['applet:workImg:edit']"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:workImg:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健步风采照片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id"/>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="照片" prop="images">
          <el-input v-model="form.images" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="已经奖励" prop="isGrade">
          <el-input v-model="form.isGrade" placeholder="请输入已经奖励"/>
        </el-form-item>
        <el-form-item label="奖励时间" prop="gradeTime">
          <el-date-picker clearable
                          v-model="form.gradeTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择奖励时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addWorkImg,
  changeWorkImgStatus,
  delWorkImg,
  getWorkImg,
  listWorkImg,
  updateWorkImg
} from "@/api/background/workImg";
import {changeCompanyRulesStatus} from "@/api/background/companyRules";

export default {
  name: "WorkImg",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健步风采照片表格数据
      workImgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        nickName: null,
        deptName: null,
        status: null,
        isGrade: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleStatusChange(row) {
      let text = row.status == 0 ? "展示" : "不展示";
      this.$modal.confirm('确认要' + text + '编号为 ' + row.id + ' 的活动风采吗？').then(function () {
        return changeWorkImgStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList();
      }).catch(function () {
        row.status = row.status == 0 ? "1" : "0";
      });
    },
    /** 查询健步风采照片列表 */
    getList() {
      this.loading = true;
      listWorkImg(this.queryParams).then(response => {
        this.workImgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        deptId: null,
        content: null,
        images: null,
        status: null,
        isGrade: null,
        gradeTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健步风采照片";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkImg(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改健步风采照片";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkImg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkImg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除健步风采照片编号为"' + ids + '"的数据项？').then(function () {
        return delWorkImg(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/workImg/export', {
        ...this.queryParams
      }, `workImg_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
